package com.itwlj.controller.user;

import com.itwlj.entity.Banner;
import com.itwlj.entity.Category;
import com.itwlj.entity.Goods;
import com.itwlj.entity.SubCategory;
import com.itwlj.result.Result;
import com.itwlj.service.BannerService;
import com.itwlj.service.CategoryService;
import com.itwlj.service.GoodsService;
import com.itwlj.service.SubCategoryService;
import com.itwlj.vo.HomeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取首页数据
 */
@RestController
@RequestMapping("/home")
@Api(tags = "C端首页相关接口")
@Slf4j
public class HomeController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SubCategoryService subCategoryService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BannerService bannerService;

    /**
     * 获取首页信息
     *
     * @return
     */
    @GetMapping()
    @ApiOperation("获取首页信息")
    public Result<List<HomeVO>> getHomeData() {
        log.info("获取分类及其相关信息...");

        List<HomeVO> homeVOList = new ArrayList<>();

        //获取分类列表
        List<Category> categoryList = categoryService.list();
        categoryList.forEach(category -> {

            //获取当前分类
            List<SubCategory> subCategoryList = subCategoryService.getByCategoryId(category.getId());
            subCategoryList = subCategoryList.size() > 2 ? subCategoryList.subList(0, 2) : subCategoryList;

            List<Goods> goodsList = new ArrayList<>();
            subCategoryList.forEach(subCategory -> {
                //获取当前子分类所有商品
                goodsList.addAll(goodsService.getBySubCategoryId(subCategory.getId()));
            });

            HomeVO homeVO = HomeVO.builder()
                    .id(category.getId())
                    .name(category.getName())
                    .subCategoryList(subCategoryList)
                    .goodsList(goodsList)
                    .build();

            homeVOList.add(homeVO);
        });

        return Result.success(homeVOList);
    }

    /**
     * 根据分发站点信息获取轮播图
     *
     * @param distributionSite
     * @return
     */
    @GetMapping("/banner")
    @ApiOperation("获取轮播图")
    public Result<List<Banner>> getBanner(Integer distributionSite) {
        log.info("获取轮播图,distributionSite：{}", distributionSite);
        List<Banner> bannerList = bannerService.getByDistributionSite(distributionSite);
        return Result.success(bannerList);
    }

    /**
     * 根据关键词搜索商品
     *
     * @param keyword
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("搜索商品")
    public Result<List<Goods>> search(String keyword) {
        log.info("搜索商品,keyword：{}", keyword);
        return Result.success(goodsService.search(keyword));
    }
}
